import { getMovieById, getBeLiked, BeLike, NotLike } from '@/api/movie'
import { getCommentByMId, getScoreLen, insertComment } from '@/api/comment'
const state = {
  MovieInfo: {},
  commentLen: 0,
  BeLiked: false
}
const getters = {

}
const mutations = {
  GETMOVIEINFO (state, movieInfo) {
    state.MovieInfo = movieInfo
  },
  GETCOMMENT (state, comment) {
    state.comment = comment
  },
  GETCOMMENTLEN (state, commentLen) {
    state.commentLen = commentLen
  },
  GETBELIKED (state, BeLiked) {
    state.BeLiked = BeLiked
  },
  NOTLIKE (state, NOTLIKE) {
    if (NOTLIKE === '取消收藏成功') {
      state.BeLiked = !state.BeLiked
    }
  },
  BELIKE (state, BELIKE) {
    if (BELIKE === '收藏成功') {
      state.BeLiked = !state.BeLiked
    }
  }
}
const actions = {
// 获取电影详情的action
  async getMovieInfo ({ commit }, mId) {
    const result = await getMovieById(mId)
    console.log(result.data)
    commit('GETMOVIEINFO', result.data)
  },
  async getComment ({ commit }, mId) {
  // 获取用户对于电影的评价
    const result = await getCommentByMId(mId)
    commit('GETCOMMENT', result.data)
  },
  async getScoreLen ({ commit }, mId) {
    const result = await getScoreLen(mId)
    commit('GETCOMMENTLEN', result.data)
  },
  async getBeLiked ({ commit }, mId) {
    const result = await getBeLiked(mId)
    commit('GETBELIKED', result.data.BeLiked)
  },
  async BeLike ({ commit }, mId) {
    const result = await BeLike(mId)
    commit('BELIKE', result.data)
  },
  async NotLike ({ commit }, mId) {
    const result = await NotLike(mId)
    commit('NOTLIKE', result.data)
  },
  async insertComment ({ commit }, data) {
    await insertComment(data)
    commit('INSERTCOMMENT', data)
  }
}
export default ({
  namespaced: true,
  state,
  getters,
  mutations,
  actions
})
